package com.jd.jr.pay.demo.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jd.jr.pay.demo.model.CreateOrderVO;
import com.jd.jr.pay.demo.util.MD5Util;
import com.jd.jr.pay.demo.util.PropertyUtils;
import com.jd.jr.pay.demo.util.StringEscape;

@Controller
public class PayController {

	private static final Logger logger = Logger.getLogger(PayController.class);

	/**
	 * 跳转到填写订单信息开始页面
	 * @param httpServletRequest
	 * @return
	 */
	@RequestMapping(value = "/getPayStart.htm")
	public String showPayPage(HttpServletRequest httpServletRequest) {
		httpServletRequest.setAttribute("mercId", PropertyUtils.getProperty("9f.mercId"));
		httpServletRequest.setAttribute("mercOrderNo", DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"));
		httpServletRequest.setAttribute("userMbl", PropertyUtils.getProperty("9f.user.uMblNo"));
		httpServletRequest.setAttribute("pageUrl", PropertyUtils.getProperty("9f.merc.pageUrl"));
		httpServletRequest.setAttribute("notifyUrl", PropertyUtils.getProperty("9f.merc.notifyUrl"));
		return "payStart";
	}

	/**
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * 拼接form提交信息的参数，并传递给自提交的页面中
	 */
	@RequestMapping(value = "/clientOrderAuto.htm")
	public String pay(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CreateOrderVO createOrderVO = new CreateOrderVO();
		
		createOrderVO.setCharset(request.getParameter("charset"));
		createOrderVO.setMercId(request.getParameter("mercId"));
		createOrderVO.setInterfaceName(request.getParameter("interfaceName"));
		createOrderVO.setVersion(request.getParameter("version"));
		createOrderVO.setSignType(request.getParameter("signType"));
		createOrderVO.setMercOrderNo(request.getParameter("mercOrderNo"));
		createOrderVO.setAmount(request.getParameter("amount"));
		createOrderVO.setValidTime(request.getParameter("validTime"));
		createOrderVO.setDescription(request.getParameter("description"));
		createOrderVO.setUserName(request.getParameter("userName"));
		createOrderVO.setUserAddr(request.getParameter("userAddr"));
		createOrderVO.setUserMbl(request.getParameter("userMbl"));
		createOrderVO.setPageUrl(request.getParameter("pageUrl"));
		createOrderVO.setNotifyUrl(request.getParameter("notifyUrl"));
		
		//过滤null值
		filterCreateOrderVO(createOrderVO);
		
		//生成验签
		String paySignString = getCreateOrderSign(createOrderVO, PropertyUtils.getProperty("9f.salt"));
		createOrderVO.setHmac(MD5Util.md5(paySignString).toLowerCase());
		
		
		request.setAttribute("payOrderInfo", createOrderVO);
		request.setAttribute("payUrl", PropertyUtils.getProperty("9f.payURL"));

		return "autoSubmit";
	}


	
	/**
	 * 
	 * @Title: filterCharProcess
	 * @Description: 对值中的<>*&特殊字符进行转义
	 * @param: @param basePayOrderInfo
	 * @return: void
	 * @throws
	 * @author mythling
	 * @Date 2016年8月6日 下午3:55:17
	 */
	private void filterCreateOrderVO(CreateOrderVO createOrderVO) {
		createOrderVO.setCharset(this.doFilterCharProcess(createOrderVO.getCharset()));
		createOrderVO.setMercId(this.doFilterCharProcess(createOrderVO.getMercId()));
		createOrderVO.setInterfaceName(this.doFilterCharProcess(createOrderVO.getInterfaceName()));
		createOrderVO.setVersion(this.doFilterCharProcess(createOrderVO.getVersion()));
		createOrderVO.setSignType(this.doFilterCharProcess(createOrderVO.getSignType()));
		createOrderVO.setMercOrderNo(this.doFilterCharProcess(createOrderVO.getMercOrderNo()));
		createOrderVO.setAmount(this.doFilterCharProcess(createOrderVO.getAmount()));
		createOrderVO.setValidTime(this.doFilterCharProcess(createOrderVO.getValidTime()));
		createOrderVO.setDescription(this.doFilterCharProcess(createOrderVO.getDescription()));
		createOrderVO.setUserName(this.doFilterCharProcess(createOrderVO.getUserName()));
		createOrderVO.setUserAddr(this.doFilterCharProcess(createOrderVO.getUserAddr()));
		createOrderVO.setUserMbl(this.doFilterCharProcess(createOrderVO.getUserMbl()));
		createOrderVO.setPageUrl(this.doFilterCharProcess(createOrderVO.getPageUrl()));
		createOrderVO.setNotifyUrl(this.doFilterCharProcess(createOrderVO.getNotifyUrl()));
	}
	/**
	 * @param createOrderVO
	 * @param key
	 * @return
	 * 拼接待sign的字符串
	 */
	public String getCreateOrderSign(CreateOrderVO createOrderVO,String key){
		return createOrderVO.getCharset()+
		createOrderVO.getMercId()+
		createOrderVO.getInterfaceName()+
		createOrderVO.getVersion()+
		createOrderVO.getSignType()+
		createOrderVO.getMercOrderNo()+
		createOrderVO.getAmount()+
		createOrderVO.getValidTime()+
		createOrderVO.getDescription()+
		createOrderVO.getPageUrl()+
		createOrderVO.getNotifyUrl()+
		key;
	}

	/**
	 * 
	 * @Title: doFilterCharProcess
	 * @Description: 执行特殊字符处理
	 * @param: @param param
	 * @param: @return
	 * @return: String
	 * @throws
	 * @author mythling
	 * @Date 2016年8月6日 下午3:54:58
	 */
	private String doFilterCharProcess(String param) {
		if (param == null || param.equals("")) {
			return "";
		} else {
			return StringEscape.htmlSecurityEscape(param);
		}
	}


}
